import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/store/user'
import MainLayout from '@/layouts/MainLayout.vue'

// 路由配置
const routes = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: MainLayout,
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { requiresAuth: true, title: '仪表板' }
      },
      // 发现中心
      {
        path: 'discovery',
        redirect: '/discovery/club-square'
      },
      {
        path: 'discovery/club-square',
        name: 'ClubSquare',
        component: () => import('@/views/Discovery/ClubSquare.vue'),
        meta: { requiresAuth: true, title: '社团广场' }
      },
      {
        path: 'discovery/activity-center',
        name: 'ActivityCenter',
        component: () => import('@/views/Discovery/ActivityCenter.vue'),
        meta: { requiresAuth: true, title: '活动中心' }
      },
      // 我的工作台
      {
        path: 'my-workbench',
        redirect: '/my-workbench/my-clubs'
      },
      {
        path: 'my-workbench/my-clubs',
        name: 'MyClubs',
        component: () => import('@/views/MyWorkbench/MyClubs.vue'),
        meta: { requiresAuth: true, title: '我的社团' }
      },
      {
        path: 'my-workbench/my-activities',
        name: 'MyActivities',
        component: () => import('@/views/MyWorkbench/MyActivities.vue'),
        meta: { requiresAuth: true, title: '我的活动' }
      },
      {
        path: 'my-workbench/my-applications',
        name: 'MyApplications',
        component: () => import('@/views/MyWorkbench/MyApplications.vue'),
        meta: { requiresAuth: true, title: '我的申请' }
      },
      // 社团管理
      {
        path: 'club-management',
        redirect: '/club-management/profile'
      },
      {
        path: 'club-management/profile',
        name: 'ClubProfile',
        component: () => import('@/views/ClubManagement/Profile.vue'),
        meta: { requiresAuth: true, title: '信息维护', roles: ['ClubAdmin', 'SuperAdmin'] }
      },
      {
        path: 'club-management/members',
        name: 'ClubMembers',
        component: () => import('@/views/ClubManagement/Members.vue'),
        meta: { requiresAuth: true, title: '成员管理', roles: ['ClubAdmin', 'SuperAdmin'] }
      },
      {
        path: 'club-management/events',
        name: 'ClubEvents',
        component: () => import('@/views/ClubManagement/Events.vue'),
        meta: { requiresAuth: true, title: '活动管理', roles: ['ClubAdmin', 'SuperAdmin'] }
      },
      {
        path: 'club-management/finances',
        name: 'ClubFinances',
        component: () => import('@/views/ClubManagement/Finances.vue'),
        meta: { requiresAuth: true, title: '财务管理', roles: ['ClubAdmin', 'SuperAdmin'] }
      },
      {
        path: 'club-management/announcements',
        name: 'ClubAnnouncements',
        component: () => import('@/views/ClubManagement/Announcements.vue'),
        meta: { requiresAuth: true, title: '公告发布', roles: ['ClubAdmin', 'SuperAdmin'] }
      },
      // 平台管理
      {
        path: 'platform-management',
        redirect: '/platform-management/user-management'
      },
      {
        path: 'platform-management/user-management',
        name: 'UserManagement',
        component: () => import('@/views/PlatformManagement/UserManagement.vue'),
        meta: { requiresAuth: true, title: '用户管理', roles: ['SuperAdmin'] }
      },
      {
        path: 'platform-management/club-management',
        redirect: '/platform-management/club-management/club-list'
      },
      {
        path: 'platform-management/club-management/club-list',
        name: 'ClubList',
        component: () => import('@/views/PlatformManagement/ClubList.vue'),
        meta: { requiresAuth: true, title: '社团列表', roles: ['SuperAdmin'] }
      },
      {
        path: 'platform-management/club-management/creation-approvals',
        name: 'CreationApprovals',
        component: () => import('@/views/PlatformManagement/CreationApprovals.vue'),
        meta: { requiresAuth: true, title: '创建审批', roles: ['SuperAdmin'] }
      },
      {
        path: 'platform-management/club-management/event-approval',
        name: 'EventApproval',
        component: () => import('@/views/PlatformManagement/EventApproval.vue'),
        meta: { requiresAuth: true, title: '活动审批', roles: ['SuperAdmin'] }
      },
      {
        path: 'platform-management/global-announcements',
        name: 'GlobalAnnouncements',
        component: () => import('@/views/PlatformManagement/GlobalAnnouncements.vue'),
        meta: { requiresAuth: true, title: '全局公告', roles: ['SuperAdmin'] }
      },
      {
        path: 'platform-management/system-settings',
        redirect: '/platform-management/system-settings/roles-permissions'
      },
      {
        path: 'platform-management/system-settings/roles-permissions',
        name: 'RolesPermissions',
        component: () => import('@/views/PlatformManagement/RolesPermissions.vue'),
        meta: { requiresAuth: true, title: '角色与权限', roles: ['SuperAdmin'] }
      },
      {
        path: 'platform-management/system-settings/category-management',
        name: 'CategoryManagement',
        component: () => import('@/views/PlatformManagement/CategoryManagement.vue'),
        meta: { requiresAuth: true, title: '分类管理', roles: ['SuperAdmin'] }
      },
      // 数据洞察
      {
        path: 'analytics',
        redirect: '/analytics/my-club-analytics'
      },
      {
        path: 'analytics/my-club-analytics',
        name: 'MyClubAnalytics',
        component: () => import('@/views/Analytics/MyClubAnalytics.vue'),
        meta: { requiresAuth: true, title: '本社团数据', roles: ['ClubAdmin', 'SuperAdmin'] }
      },
      {
        path: 'analytics/platform-analytics',
        name: 'PlatformAnalytics',
        component: () => import('@/views/Analytics/PlatformAnalytics.vue'),
        meta: { requiresAuth: true, title: '全平台数据', roles: ['SuperAdmin'] }
      }
    ]
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  // 如果路由需要认证
  if (to.meta.requiresAuth) {
    // 检查是否已登录
    if (!userStore.isLoggedIn) {
      next('/login')
      return
    }
    
    // 检查角色权限
    if (to.meta.roles && !to.meta.roles.includes(userStore.role)) {
      // 权限不足，重定向到仪表板
      next('/dashboard')
      return
    }
  }
  
  // 如果已登录且访问登录页，重定向到仪表板
  if (to.path === '/login' && userStore.isLoggedIn) {
    next('/dashboard')
    return
  }
  
  next()
})

export default router
